package com.magicborrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.magicborrow.Constants;
import com.magicborrow.MagicBorrowApplication;
import com.magicborrow.R;
import com.magicborrow.beans.NormalBean1;
import com.magicborrow.beans.UserBean;
import com.magicborrow.utils.MD5;
import com.magicborrow.utils.UserTools;
import com.magicborrow.utils.VolleyTool;
import com.magicborrow.views.LoadingDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/11.
 */
public class WeChatPhoneActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout ll_frame;
    private TextView tv_confirm;
    private ImageView iv_back;
    private TextView tv_send;
    private EditText edit_phone;
    private EditText edit_code;
    private EditText edit_password;
    private String phone;
    private String checkCode;
    private String password;
    private LoadingDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registers);
        initView();
        initListener();

        initDate();
    }

    private void initDate() {


    }

    private void initView() {
        //如果验证码错误需要修改边框颜色就修改这个LinearLayout的背景颜色
        ll_frame = (LinearLayout) findViewById(R.id.ll_frame);
        tv_confirm = (TextView) findViewById(R.id.tv_confirm);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        tv_send = (TextView) findViewById(R.id.tv_send);
        edit_phone = (EditText) findViewById(R.id.edit_phone);
        edit_code = (EditText) findViewById(R.id.edit_code);
        edit_password = (EditText) findViewById(R.id.edit_password);
        ((TextView) findViewById(R.id.tv_title)).setText("绑定手机号");
    }

    private void initListener() {
        tv_confirm.setOnClickListener(this);
        iv_back.setOnClickListener(this);
        tv_send.setOnClickListener(this);
    }

    private void checkCode() {
        if (!checkPhone()) {
            return;
        }
        if (!checkCheckCode()) {
            return;
        }
        if (!checkPassword()) {
            return;
        }
        dialog = new LoadingDialog(this);
        dialog.show();
        Map<String, String> params = new HashMap<>();
        params.put("mobile", phone);
        params.put("code", checkCode);
        params.put("acc_token", UserTools.getUser(this).getAcc_token());
        params.put("password", password);
        VolleyTool.get(Constants.CHECK_WE_CHAT_PHONE, params, new VolleyTool.HTTPListener() {
            @Override
            public <T> void onResponse(T t, int requestCode) {
                dialog.dismiss();
                UserBean bean = (UserBean) t;

                if (bean.getCode() == 0) {
                   // final UserBean.User user = bean.getData();
                   /* EMChatManager.getInstance().login(user.getId() + "", MD5.getMD5(MD5.getMD5(password + "")), new EMCallBack() {
                        @Override
                        public void onSuccess() {
                            //  Toast.makeText(TaPageActivity.this, "chenggong" + MD5.getMD5(MD5.getMD5(user.getPassword())), Toast.LENGTH_SHORT).show();
                            // 登陆成功，保存用户名密码
                            MagicBorrowApplication.getInstance().setUserName(user.getId() + "");
                            MagicBorrowApplication.getInstance().setPassword(MD5.getMD5(MD5.getMD5(user.getId() + "")));

                            // 更新当前用户的nickname 此方法的作用是在ios离线推送时能够显示用户nick
                            boolean updatenick = EMChatManager.getInstance().updateCurrentUserNick(
                                    user.getNickname());
                            Log.e("环信登录成功", "sssss  " + user.getId() + "  ");
                        }

                        int t;

                        @Override
                        public void onError(int i, String s) {
                            Log.e("xxxx", s);
                            if (t < 4) {
                                EMChatManager.getInstance().login(user.getId() + "", MD5.getMD5(MD5.getMD5(password)), this);
                                t++;
                            }
                        }

                        @Override
                        public void onProgress(int i, String s) {

                        }
                    });*/
                    Intent intent = new Intent(WeChatPhoneActivity.this, MainActivity.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(WeChatPhoneActivity.this, bean.getMessage(), Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onErrorResponse(VolleyError error, int requestCode) {
                dialog.dismiss();
                Toast.makeText(WeChatPhoneActivity.this, "服务器忙,请稍微再试", Toast.LENGTH_SHORT).show();
            }
        }, 0, UserBean.class);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_confirm:
                checkCode();
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_send:
                if (!checkPhone()) {
                    return;
                }
                Map<String, String> params = new HashMap<>();
                params.put("mobile", phone);
                params.put("type", "2");
                params.put("channel", "1");
                VolleyTool.get(Constants.GET_CHECK_CODE, params, new VolleyTool.HTTPListener() {
                    @Override
                    public <T> void onResponse(T t, int requestCode) {
                        NormalBean1 bean = (NormalBean1) t;
                        if (bean.getCode() == 0) {
                            Toast.makeText(WeChatPhoneActivity.this, "短信已发送到您的手机,请查收", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(WeChatPhoneActivity.this, bean.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onErrorResponse(VolleyError error, int requestCode) {
                        Toast.makeText(WeChatPhoneActivity.this, "服务器忙,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                }, 1, NormalBean1.class);

                break;
        }
    }

    private boolean checkPhone() {
        phone = edit_phone.getText().toString();
        if ("".equals(phone)) {
            Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (phone.length() < 11) {
            Toast.makeText(this, "请正确填写手机号码", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private boolean checkCheckCode() {
        checkCode = edit_code.getText().toString();
        if ("".equals(checkCode)) {
            Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private boolean checkPassword() {
        password = edit_password.getText().toString();
        if ("".equals(password)) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (phone.length() < 6) {
            Toast.makeText(this, "密码不能低于6位数", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

}
